<?php
	session_start();

  include_once("datastore.inc.php");
	include_once("filesystem.inc.php");

  $storageFile = GetStorageFile();

  function getNewContact() {
    $contact = array(
      "id"   				 => "",
      "groupName"    => "",
      "name_first"   => "",
      "name_last"    => "",
      "name_nick"    => "",
      "birthday"     => "",
      "street"       => "",
      "post_code"    => "",
      "place"        => "",
      "state"        => "",
      "country"      => "",
      "phone_home"   => "",
      "phone_mobile" => "",
      "phone_work"   => "",
      "mail_first"   => "",
      "mail_second"  => "",
      "im_jabber"    => "",
      "im_icq"       => "",
      "homepage"     => "",
      "notes"        => ""
    );

    return $contact;
  }

  function getNewGroup() {
    $group = array(
      "id"     => "",
      "name"   => ""
    );

    return $group;
  }

  function loadContacts() {
  	global $storageFile, $reqUsername, $reqPassword, $reqGroup, $reqContact;

  	$contacts = dataStore_loadContacts($storageFile, $reqUsername, $reqPassword, $reqGroup, $reqContact);

    return $contacts;
  }

  function login()
  {
  	global $storageFile, $reqUsername, $reqPassword;

  	$res = dataStore_userExists($storageFile, $reqUsername, $reqPassword);

  	$_SESSION['username'] = $reqUsername;
  	$_SESSION['password'] = $reqPassword;

  	return $res;
  }

  function logout()
  {
  	global $reqUsername, $reqPassword;

  	unset($_SESSION['username']);
  	unset($_SESSION['password']);

  	return "<result>true</result>";
  }

  function addContactData()
  {
  	global $storageFile, $reqUsername, $reqPassword;

  	$newContact = getNewContact();

  	foreach ($_POST as $fieldname => $fieldvalue)
  	{
  		$newContact[$fieldname] = $fieldvalue;
  	}

  	$res = dataStore_AddContact($storageFile, $reqUsername, $reqPassword, $newContact);

  	return $res;
  }

  function deleteContactData()
  {
  	global $storageFile, $reqUsername, $reqPassword;

  	$res = '';
  	$contactId = $_GET['contactid'];
  	if ($contactId && $contactId != '' && $contactId != null)
  	{
  		$res = dataStore_DeleteContact($storageFile, $reqUsername, $reqPassword, $contactId);
  	}

  	return $res;
  }

  function isLoggedin()
  {
  	$result = "<result>false</result>";

  	if (isset($_SESSION['username']) && isset($_SESSION['password']))
  	{
  		$result = "<result>true</result>";
  	}

  	return $result;
  }

  function importData()
  {
  	global $storageFile, $reqUsername, $reqPassword;

  	if ($_FILES['importfile'] != null && $_FILES['importfile']['tmp_name'] != null)
  	{
			$filename = $_FILES['importfile']['name'];
			move_uploaded_file($_FILES['importfile'], "../data/".$filename);
			$info = pathinfo( $filename );

			$contacts = loadContactFile($info['extension'], '../data/'.$filename);

			if ($contacts && count($contacts) > 0)
			{
				dataStore_saveXML($storageFile, $reqUsername, $reqPassword, $contacts);

				echo '"'.$filename .'" succeesfuly loaded';
			}
  	}
  }

  function exportData()
  {
  	global $reqUsername, $reqPassword;

  	//TODO
  }

  function loadContactFile($ext, $filename) {
    switch ($ext) {
    case "ldif":
      include_once("ldif.inc.php");
      $contacts = loadLDIF($filename);
      break;
    case "vcf" :
      include_once("vcf.inc.php");
      $contacts = loadVCF($filename);
      break;
    }

    return $contacts;
  }

  function saveContacts($contacts, $file, $file) {
    $info = pathinfo($file);
    $ext  = strtolower($info["extension"]);

    switch ($ext) {
    case "ldif":
      include_once("ldif.inc.php");
      saveLDIF($contacts, $file);
      break;
    case "vcf" :
      include_once("vcf.inc.php");
      saveVCF($contacts, $file);
      break;
    }
  }

  function exportXML($contacts) {
    $doc = new DomDocument("1.0");

    $root = $doc->createElement("contacts");
    $root = $doc->appendChild($root);

    foreach ($contacts as $contact) {
      $row = $doc->createElement("contact");
      $row = $root->appendChild($row);

      foreach (array_keys($contact) as $key) {

        if ($contact[$key]) {
          $col = $doc->createElement($key, $contact[$key]);
          $col = $row->appendChild($col);
        }
      }
    }

    $doc->formatOutput = true;
    return $doc->saveXML();
  }

	function GetStorageFile()
	{
		$result = "";

		$files = listDir("../data/");

		for ($i = 0; $i <count($files); $i++)
		{
			$info = pathinfo("../data/".$files[$i]);
			if ($info['extension'] == 'xml')
			{
				$result = "../data/".$files[$i];
				break;
			}
		}

		return $result;
	}
?>